package com.service.tcp.Tcp_doing;

import com.domain.Box_data;
import com.domain.Tcp_public_data;
import com.mapper.Box_mysql;
import com.mapper.Place_mysql;
import com.service.Checker;
import com.service.Public_funtion;
import com.service.tcp.Tcp_public_funtion;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import org.springframework.beans.factory.annotation.Autowired;

import static com.MysqlApplication.equip_list;

public class Web_to_Tcp_doing {



    @Autowired
    private Tcp_public_funtion tcp_p;
    @Autowired
    private Public_funtion p;
    @Autowired
    private Box_mysql box_mysql;
    @Autowired
    private Place_mysql place_mysql;
    @Autowired
    private Checker checker;

    public  void  doing(Box_data data)
    {
        //检查发来Boxdata的数据的正确性
        //打开柜门
        //
        //

        String check=checker.check_box_data(data);
        if (check.equals("数据格式正确"))
        {
            int locker_id=data.getLocker_id();
            String locker_id_str=String.valueOf(locker_id);
            String place_id=locker_id_str.substring(0,3);

            Tcp_public_data tcp_data=new Tcp_public_data();
            tcp_data.setSpeed(data.getLocker_id());
            for(int i=0;i<equip_list.size();i++)
            {
                if (equip_list.get(i).getPlace_id()==place_id)
                {
                    ChannelHandlerContext ctx= equip_list.get(i).getEquip();
                    ByteBuf encoded=tcp_p.tcp_to_bytebuf(ctx,tcp_data);
                    ctx.channel().writeAndFlush(encoded).syncUninterruptibly();
                }
            }
            p.print_log("网页远程打开成功", 5);
            p.print_log(data.toString(), 7);
        } else {
            p.print_log("网页远程打开错误，错误报告为"+check,5);
        }
    }
}
